﻿App.KnowledgeDetailTypeController = Ember.ObjectController.extend({
    actions: {
        addChild: function () {
            var model = this.get('model');
            var newModel = this.store.find(App.KnowledgeDetailType, 0);
            //if (newModel) {
            //    newModel = this.store.createRecord(App.KnowledgeDetailType);
            //}
            newModel.set('ParentCode', (model.get('ParentCode') ? model.get('ParentCode') + '.' : '') + model.get('Code'));
            this.transitionToRoute('knowledgedetailtype.edittype', 0);
        },
        edit: function () {
            var model = this.get('model');
            this.transitionToRoute('knowledgedetailtype.edittype', model.get('id'));
        },
        del: function () {
            var that = this;
            var model = this.get('model');

            Ember.$.model.del('KnowledgeDetailType', model, function (id) {
                model.deleteRecord();
                model.save();

                that.transitionToRoute('knowledgedetailtype', this.knowledgeDetail_Name);
            });
        },
        linkToKnowledgeDetail: function () {
            var model = this.get('model');
            this.transitionToRoute('knowledgedetailtype.knowledgedetail', model.get('id'));
        }
    },
    knowledgeDetail_Name: function () {
        return Type().Name;
    },
    before: function () {
        var parentCode = this.get('model').get('ParentCode');
        if (parentCode) {
            var len = parentCode.split('.').length + 1;
            return new Array(len).join('—');
        } else {
            return '';
        }
    }.property('model.ParentCode')
});

